package com.jarvis.product.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import cn.hutool.core.util.StrUtil;
import com.jarvis.product.entity.ProductEntity;
import com.jarvis.product.service.ApplePayService;
import com.jarvis.product.service.ProductService;
import com.jarvis.product.util.ApplePayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.jarvis.product.entity.OrderEntity;
import com.jarvis.product.service.OrderService;
import com.jarvis.common.utils.PageUtils;
import com.jarvis.common.utils.R;

import com.jarvis.product.feign.UserFeignService;

/**
 * 订单
 *
 * @author gongxin
 * @email 19910320678@163.com
 * @date 2023-04-01 22:57:58
 */
@RestController
@RequestMapping("product/order")
public class OrderController {
    @Autowired
    private OrderService orderService;


    @Autowired
    private ProductService productService;
    @Autowired
    private UserFeignService userFeignService;

    @Autowired
    ApplePayService applePayService;

    @Autowired
    ApplePayUtil applePayUtil;


//    private static ApplePayUtil applePayUtil;

//    @Autowired
//    public void setApplePayUtil(ApplePayUtil applePayUtil) {
//        OrderController.applePayUtil = applePayUtil;
//    }

    /**
     * @param :TransactionID订单号
     * @param :receipt订单加密收据
     * @Description: Ios客户端内购支付
     */
    @RequestMapping("/applePay")
    public R doIosRequest(@RequestParam("receipt") String receipt, @RequestParam("TransactionID") String TransactionID,@RequestParam("productId") long productId, @RequestHeader Map<String, String> headers) {

        String verifyResult = applePayUtil.buyAppVerify(receipt, 0);

        //查询用户
        String uid = headers.get("uid");
        if (StrUtil.isBlank(uid)) {
            return R.error(500,"服务器异常");
        }

        //获取要购买的商品
        ProductEntity productEntity = productService.getProduct(productId);


        //苹果服务器没有返回验证结果
        if (applePayService.getAppPay(verifyResult, TransactionID)) {

            //验证成功增加用户会员市场
            return userFeignService.addVipDay(Long.parseLong(uid),productEntity.getVipDay());

        } else {

            return R.error(500,"充值失败");

        }
    }




    /**
     * 列表
     */
    @RequestMapping("/list")
  //  @RequiresPermissions("product:order:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
   // @RequiresPermissions("product:order:info")
    public R info(@PathVariable("id") Long id){
		OrderEntity order = orderService.getById(id);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
  //  @RequiresPermissions("product:order:save")
    public R save(@RequestBody OrderEntity order){
		orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
  //  @RequiresPermissions("product:order:update")
    public R update(@RequestBody OrderEntity order){
		orderService.updateById(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("product:order:delete")
    public R delete(@RequestBody Long[] ids){
		orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
